package com.yan.config;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * @author yanchen
 */
@Configuration
public class ValidatorConfiguration {

    @Bean
    public Validator validator() {
        // 获取校验器配置类
        HibernateValidatorConfiguration configure =
            Validation.byProvider(HibernateValidator.class).configure();
        // 快速失败模式开启
        // SpringValidation框架默认是关闭快速失败的，也就是说当一个参数校验不通过的时候它不会直接停止，而是会继续校验剩余的参数
        configure.failFast(true);

        // 构建验证器工厂
        ValidatorFactory factory = configure.buildValidatorFactory();
        // 从验证器工厂中制造一个验证其实例，并返回到 Bean 容器中被管理
        return factory.getValidator();
    }

}
